package com.huatao.subject.application.controller;

import com.google.common.base.Preconditions;
import com.huatao.subject.application.convert.SubjectLabelDTOConvert;
import com.huatao.subject.application.dto.SubjectLabelDTO;
import com.huatao.subject.common.entity.Result;
import com.huatao.subject.domain.entity.SubjectLabelBO;
import com.huatao.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {

    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController add subjectLabelDTO: {}", subjectLabelDTO);
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectLabelDTO.getLabelName()), "标签名称不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConvert.INSTANCE.convertDtoToBo(subjectLabelDTO);
            Boolean res = subjectLabelDomainService.add(subjectLabelBO);
            return Result.ok(res);
        } catch (Exception e) {
            log.error("SubjectLabelController add error: {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController update subjectLabelDTO: {}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConvert.INSTANCE.convertDtoToBo(subjectLabelDTO);
            Boolean res = subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok(res);
        } catch (Exception e) {
            log.error("SubjectLabelController add error: {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController delete subjectLabelDTO: {}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConvert.INSTANCE.convertDtoToBo(subjectLabelDTO);
            Boolean res = subjectLabelDomainService.delete(subjectLabelBO);
            return Result.ok(res);
        } catch (Exception e) {
            log.error("SubjectLabelController add error: {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController queryLabelByCategoryId subjectLabelDTO: {}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类Id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConvert.INSTANCE.convertDtoToBo(subjectLabelDTO);
            List<SubjectLabelBO> boList = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO> res = SubjectLabelDTOConvert.INSTANCE.convertBoToDto(boList);
            return Result.ok(res);
        } catch (Exception e) {
            log.error("SubjectLabelController queryLabelByCategoryId error: {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

}
